package com.dqp.cslggroup.UI;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.Api.schoolApi;
import com.dqp.cslggroup.ConnectSever.SeverIP;
import com.dqp.cslggroup.EventBus.MessageEvent;
import com.dqp.cslggroup.MainActivity;
import com.dqp.cslggroup.Util.Result;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.Util.ToastUtil;
import com.dqp.cslggroup.bean.Calendar;
import com.dqp.cslggroup.newStudent;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import pub.devrel.easypermissions.EasyPermissions;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SplashActivity extends BaseActivity {
    ExecutorService threadPool = Executors.newFixedThreadPool(5);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setIP();
    }

    public void welcom() {
        String xxmh = SharedPreferencesUtil.getStringSp(this, "password_XXMH", null);
        String sno = SharedPreferencesUtil.getStringSp(this, "stuNum", null);
        String jwxt = SharedPreferencesUtil.getStringSp(this, "password_JWXT", null);
        if (xxmh == null || sno == null || sno.length() != 9) {
            startActivity(new Intent(this, newStudent.class));
            finish();
        } else {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    public void setIP() {
        threadPool.execute(() -> {
            JSONObject sever = SeverIP.getIp();
            if (sever == null) sever = SeverIP.getIpByGitee();
            if (sever != null && sever.containsKey("ip")) {
                JSONArray array = sever.getJSONArray("ip");
                if (array.size() > 0) {
                    Random random = new Random();
                    int randomNum = random.nextInt(array.size());
                    String ip = array.get(randomNum).toString();
                    SharedPreferencesUtil.putStringSp(this, "ip", ip.replace(" ", ""));
//                    SharedPreferencesUtil.putStringSp(this, "ip", " http://3yuqvk.natappfree.cc".replace(" ", ""));
                    if (!StringUtil.isEmpty(ip, 5)) {
                        JSONObject jsonObject = schoolApi.schoolCalendar();
                        if (Result.jsonResultIsTure(jsonObject)) {
                            assert jsonObject != null;
                            Calendar calendar = jsonObject.toJavaObject(Calendar.class);
                            MyApplication.getDaoInstant().getCalendarDao().insertOrReplace(calendar);
                            SharedPreferencesUtil.putStringSp(this, "Start_date", calendar.getStarttime());
                        }
                    }
                }
            } else {
                SharedPreferencesUtil.putStringSp(this, "ip", "https://www.cslggroup.top/imk");
                EventBus.getDefault().post(new MessageEvent("SplashActivity", 0, "连接服务器失败"));
            }
            EventBus.getDefault().post(new MessageEvent("SplashActivity", 1));
        });
    }

    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getEvent().equals("SplashActivity")) {
            switch (event.getWhat()) {
                case 0:
                    ToastUtil.showToast(event.getMessage());
                    break;
                case 1:
                    welcom();
                    break;
            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
